// pages/productDetails/productDetails.js
const app = getApp()
var api = require('../../../utils/api.js')
var util = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
     numbers: 1,
    modifyPriceCol: true,
    share:1,
    share_id:'',
    cartCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.hideShareMenu() // 隐藏分享按钮
    
    var that = this;
    wx.getUserInfo({
      success: function (res) {
        that.setData({
          userInfo: res.userInfo
        })
      }
    })

    if (options.sku_code) {
      this.setData({
        sku_code: options.sku_code
      })
    }

    if (options.uid) {
      this.setData({
        uid: options.uid
      })
    }

    if (options.share_id) {
      this.setData({
        share_id: options.share_id
      })
    }

    if (options.q) {
      var link = options.q;
      link = unescape(decodeURI(link));
       var num = link.indexOf("?");
       link = link.substring(num+1);
       var ary = link.split("&");
       var obj = {}
       for (var i = 0; i < ary.length;i++){
        var key = ary[i].split("=")[0];
        var value = ary[i].split("=")[1];
        obj[key] = value
       }
       this.setData(obj)
    }


    // this.getProductInfo();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getProductInfo();
    this.cartNum();
  },

  getProductInfo: function() {
    var that = this;
    var params = new Object()
    params.share_id = this.data.share_id;
    
    util.showLoading("数据加载中..");
    api.POST({
      params: params,
      url: app.globalData.base_url + 'goods/shareInfo',
      success: function(res) {
        util.hiddenLoading();
        if (util.isEmpty(res.data.code)) {
          that.setData({
            rawData: res.data.data,
            newPrice: res.data.data.current_price
          });
        }

      },
      fail: function(fail) {
        util.showErrorPath("获取失败，请检查网络");
      }
    })
  },

   //手动输入数量
   inputNumber: function (e) {
      var num = e.detail.value;
      num = num.replace(/^0/g, '');
      var stock = this.data.rawData.stock / 1;
      if (stock && num != '') {
         if (num >= stock) {
            util.showError("库存不足")
         }
         num <= stock ? num = num : num = stock
      }
      this.setData({
         numbers: num
      })
   },

   //检查输入数量是否为空
   checkInputNumber: function () {
      var num = this.data.numbers;
      var reg = /^\s*$/g;
      if (reg.test(num) || num == null || num == "") {
         this.setData({
            numbers: 1
         })
      }
   },


  addNum: function() {
    var num = this.data.numbers;
    num++;
    this.setData({
      numbers: num
    })
  },
  minusNum: function() {
    var num = this.data.numbers;
    if (num > 1) {
      num--;
    }
    this.setData({
      numbers: num
    })
  },
  // 立即购买
  pay: function() {
    var orderList = [];
    var box = new Object();
    box.num = this.data.numbers;
    box.stock = this.data.rawData.stock;

    if (this.data.share_id) {
      var share_id = this.data.share_id;
    }
    if (this.data.uid) {
      var share_uid = this.data.uid;
    }

    box.sku_code = this.data.sku_code;
    box.current_price = this.data.rawData.share_price;
    box.gname = this.data.rawData.gname;
    box.image = this.data.rawData.image;
    box.degree = this.data.rawData.degree;
    box.degree_unit = this.data.rawData.degree_unit;
    box.volume = this.data.rawData.volume;
    box.volume_unit = this.data.rawData.volume_unit;
    box.total = (this.data.numbers * this.data.rawData.share_price).toFixed(2);
    orderList.push(box);
    wx.setStorageSync('orderList', orderList);
    if (share_id) {
      wx.navigateTo({
        url: '/index/pages/order/order?share_id=' + share_id + '&share_uid=' + share_uid
      })
    }
  },
  onShareAppMessage: function(res) {
    var that = this;
    var uid = '&uid=' + wx.getStorageSync('accountInfo').uid;
    var inn_id = that.data.inn_id;
    var share_id = that.data.share_id;
    if (share_id) {
      var share_id = '&share_id=' + share_id;
      var inn_id = '&inn_id=' + inn_id;
      var url = '/agent/pages/shareStoreProductDetail/shareStoreProductDetail?sku_code=' + that.data.sku_code + inn_id + share_id + uid
    }
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: that.data.rawData.gname,
        path: url,
        imageUrl: that.data.rawData.share_img,
        success: function(res) {
          // 转发成功
        },
        fail: function(res) {
          // 转发失败
        }
      }
    }
  }
})